from pyexpat.errors import messages

from . import stu_bp
from model.studb import StuInfo
from flask import request
from lib.response import response

@stu_bp.route("/student/add",methods=["POST"])
def add_student():
    StuInfo.create_stu(
        request.json.get("stuname"),
        request.json.get("stuage"),
        request.json.get("stuaddress"),
        request.json.get("stuclass")
    )
    return response(message="add student success!")

@stu_bp.route("/student/delete",methods=["DELETE"])
def del_student():
    id = request.args.get("id")
    if id:
        StuInfo.del_stu(id)
        return response(message="delete student success!")
    else:
        return response(code=101,message="参数传递不完整!")

@stu_bp.route("/student/modify",methods=['PUT'])
def modify_student():
    id = request.args.get("id")
    if id:
        StuInfo.mod_stu(
            id,
            request.json.get('stuname'),
            request.json.get('stuage'),
            request.json.get('stuaddress'),
            request.json.get('stuclass')
        )
        return response(message="modify student success!")
    else:
        return response(code=101,message="参数传递不完整")

@stu_bp.route("/get/student/")
def get_student():
    id = request.args.get("id")
    if not id:
        result = StuInfo.get_students()
    else:
        result = StuInfo.get_student(id)
    return response(data=result,message="get student info success!")







